﻿using AutoMapper;
using ImageMagick;
using Modules;
using Modules.Corp;
using Modules.Person;
using Modules.Project;
using Modules.Project.Entitys;
using Modules.System.Security;
using PmSoft;
using System.Reflection;

namespace AppWebApi.Models
{
    public class UserEmployeeDto
    {
        public int PersonId { get; set; }

        public string PersonName { get; set; }
        /// <summary>
        /// 年龄
        /// </summary>
        public int Age { get; set; }
        /// <summary>
        /// 是否认证
        /// </summary>
        public bool IsAuth { get; set; }
        public string CorpName { get; set; }

        public string WorkTypeName { get; set; }

        public string GroupName { get; set; }

        public string Mobile { get; set; }
    }

    public static partial class UserEmployeeExceptions
    {
        public static IEnumerable<UserEmployeeDto> AsDtos(this IEnumerable<ProjectGroupMember> members)
        {
            List<UserEmployeeDto> dtos = new();
            var projectGroupService = ServiceAccessor.GetService<ProjectGroupService>();
            var personService = ServiceAccessor.GetService<PersonService>();
            var securityService = ServiceAccessor.GetService<SecurityService>();
            var corpService = ServiceAccessor.GetService<CorpService>();
            var personInfos = personService.GetPersons(members.Select(a => a.PersonId));
            var groups = projectGroupService.GetProjectGroups(members.Select(a => a.GroupId));
            var securitys = securityService.GetSysSecuritys(personInfos.Where(a => a.SecurityId.HasValue == true).Select(a => a.SecurityId ?? 0));
            var corps = corpService.GetCorpInfos(members.Select(a => a.CorpId));
            foreach (var item in members)
            {
                dtos.Add(item.AsDto(personInfos, corps, groups, securitys));
            }
            return dtos;
        }

        public static UserEmployeeDto AsDto(this ProjectGroupMember member, IEnumerable<PersonInfo> personInfos, IEnumerable<CorpInfo> corps, IEnumerable<ProjectGroup> groups, IEnumerable<SysSecurity> securities)
        {
            var securityId = personInfos.FirstOrDefault(a => a.PersonId == member.PersonId)?.SecurityId ?? 0;
            UserEmployeeDto dto = new()
            {
                PersonId = member.PersonId,
                PersonName = personInfos.FirstOrDefault(a => a.PersonId == member.PersonId)?.Name ?? "",
                Age = (DateTime.Now.Year - personInfos.FirstOrDefault(a => a.PersonId == member.PersonId)?.Birthday?.Year) ?? 0,
                IsAuth = securities.FirstOrDefault(a => a.SecurityId == securityId)?.IsAuth ?? false,
                CorpName = corps.FirstOrDefault(a => a.CorpId == member.CorpId)?.Name ?? "",
                WorkTypeName = member.Type?.GetDescription() ?? "",
                GroupName = groups.FirstOrDefault(a => a.GroupId == member.GroupId)?.Name ?? "",
                Mobile = personInfos.FirstOrDefault(a => a.PersonId == member.PersonId)?.Mobile ?? "",
            };
            return dto;
        }
    }
}
